﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Standard.Dtos;
using Standard;
using EquipmentManagement.Application.IServices;
using EquipmentManagement.Domain.Entities;
using SqlSugar;

namespace EquipmentManagement.ApiHost.Controllers
{
    /// <summary>
    /// 设备台账管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeviceLedgerController : ControllerBase
    {
        private readonly IDeviceLedgerService _deviceLedger;
        private readonly ISqlSugarClient _db;

        public DeviceLedgerController(IDeviceLedgerService deviceLedger, ISqlSugarClient db)
        {
            _deviceLedger = deviceLedger;
            _db = db;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> Add(DeviceLedger model)
        {
            return _deviceLedger.Add(model);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> Update(DeviceLedger model)
        {
            return _deviceLedger.Update(model);
        }
        /// <summary>
        /// 删除(逻辑删除)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult<int> Delete(string id)
        {
            return _deviceLedger.Delete(id);
        }
        /// <summary>
        /// 获取列表(分页+条件查询)
        /// </summary>
        /// <param name="paging"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiPaging<DeviceLedger> GetList([FromQuery] PagingInput paging, string? AssetNumber, string? Code, string? Name, string? State)
        {
            return _deviceLedger.GetList(paging, AssetNumber, Code, Name, State);
        }
        /// <summary>
        /// 获取详情
        /// </summary>
        [HttpGet]
        public ApiResult<DeviceLedger> Get([FromQuery] string id)
        {
            return _deviceLedger.Get(id);
        }
        /// <summary>
        /// 导出设备台账清单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportDeviceLedgera()
        {
            try
            {
                var query = _db.Queryable<DeviceLedger>().Where(x => !x.IsDeleted);

                // 列名映射配置（实体属性名 → Excel显示名）
                var columnMappings = new Dictionary<string, string>
                {
                    { "Id", "ID" },
                    {"AssetNumber","资产编号" },
                    {"Code","编码" },
                    {"Name","名称" },
                    {"TypeId","设备类型" },
                    {"Brand","品牌" },
                    {"Model","型号" },
                    {"Specification","规格" },
                    {"Num","数量" },
                    {"SupplierId","供应商" },
                    {"WorkShopId","车间" },
                    {"DateOfManufacture","生产日期" },
                    {"EntryDateToTheFactory","入厂日期" },
                    {"Location","位置" },
                    {"DepartmentId","部门" },
                    {"PersonInChargeId","负责人" },
                    {"Remark","备注" },
                    {"State","状态" },
                    { "CreatedUserName", "创建人" },
                    { "CreatedTime", "创建时间" },
                    { "UpdateUserName", "更新人" },
                    { "UpdateTime", "更新时间" },
                };

                // 使用改进的ExportService
                var stream = new ExportService().ExportToExcel(
                    query.ToList(),  // 注意：先转换为List
                    columnMappings,
                    "设备台账清单");

                return File(stream, "application/vnd.ms-excel", "DeviceLedgers.xls");
            }
            catch (Exception ex)
            {
                return StatusCode(500, "导出过程中发生错误");
            }
        }











    }
}
